#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtNetwork>
#include <QDesktopWidget>
#include <QApplication>
#include <QMessageBox>
#include <QPushButton>
#include <QVBoxLayout>
#include <QScrollArea>
#include <QScrollBar>
#include <QFileDialog>
#include <QPixmap>
#include <QMap>
#include <QIcon>
#include <QTimer>
#include "menutop.h"
#include "logindialog.h"
#include "indexpage.h"
#include "personindex.h"
#include "pixlabel.h"
#include "publishblog.h"
#include "paydialog.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

enum TRAN_ID{
    ID_FILE=0,
    ID_MSG,
};

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    void initTCP();
    void initUI();
    //void msgBox(QString msg);
    void sendFile(QString path, QString name);
    void sendMessage(QString msg);
    void msgHandler(QString);
    void initTest();
    int mainType=0;
    QString subType="C";
    void showLogin(int idx);
    void sendToken();
    void sendFileACK(QString);
    void parseBlogList(QString, QString);
    void parseResList(QString, QString);
    void parseUserBlogList(QString);
    void parseUserResList(QString msg);
    void parseCommentList(QString);
    void showMain(QString type);
    void clearBlogMap(QString type);
    void showUserBlogList();
    void showUserResList();
    void showUserBlog();
    void showMsg();
    void closeEvent(QCloseEvent *event);
private:
    Ui::MainWindow *ui;
    QTcpSocket *tcpClientFile;
    QTcpSocket *tcpClientMessage;
    QFile *localFile;
    qint64 fileNameSize;  //文件名的大小信息
    QString fileName;  //保存文件路径
    quint16 blockSize;      //存放接收到的信息大小
    qint64 loadSize;        //每次发送数据的大小
    qint64 totalWrite[2];   //数据总大小
    qint64 bytesToWrite[2];   //剩余数据大小
    QByteArray outBlock[2];  //数据缓冲区，即存放每次要发送的数据
    qint64 totalRead[2];
    qint64 bytesReaded[2];  //已收到数据的大小
    QByteArray inBlock[2];  //数据缓冲区
    QString recvMsg;
    QList<QString> fileList;
    QList<QString> realList;
    QFile *sFile;
    bool isSendFile=false;
    QList<QString> sendMsg;
    bool isSendMsg=false;
    QString token;
    QTimer *timerFile;
    QTimer *timerMsg;

    MenuTop *menuTop;
    LoginDialog *loginDialog;
    IndexPage *index;
    QScrollArea *scrollArea;
    QScrollBar *scrollBar;
    QWidget *scrollWidget;
    QVBoxLayout *scrollLayout;
    PersonIndex *personIndex;
    PublishBlog *publishBlog;
    PayDialog *payDialog;
    QString showType="index";

    QMap<int, QMap<QString, QString>> blogMap_c,blogMap_cpp,blogMap_php,blogMap_java,blogMap_android,blogMap_harmony,blogMap_stm32;
    QMap<int, QMap<QString, QString>> resMap_c, resMap_cpp, resMap_php, resMap_java, resMap_android, resMap_harmony, resMap_stm32;
    QMap<int, QMap<QString, QString>> userBlogMap, userResMap, commentMap;
    bool blogReady = false;
    bool canDownload=false;
    QString downloadPath;
    QString praiseNum, collectionNum, commentNum;


public slots:
    void fileConnected();
    void messageConnected();
    void readFile();
    void readMessage();
    void slotBytesWritten(qint64);
    void socketErrorFile(QAbstractSocket::SocketError);
    void socketErrorMessage(QAbstractSocket::SocketError);

    void slotGotoPage(enum LabelNnum lnum);
    void slotTypeChanged(int num);
    void getDialogMsg(QString msg);
    void getIndex(QString type);
    void slotBlogOperation(QMap<QString,QString> map, QString msg);
    void slotBlogOperation(QMap<QString,QString>,int);
    void slotBlogInfo(QMap<QString,QString>,int);
    void slotDownloadRes(QMap<QString, QString> map);
    void slotUserRes(QString);
    void slotPublishBlog(QMap<QString,QString> mapInfo, QList<QString> strList);
    void slotPublishRes(QMap<QString,QString> mapInfo);
    void slotTimerFile();
    void slotTimerMsg();
    void slotPay(int);
    void slotScrollChanged(int val);
    void slotComment(QString comment);
};
#endif // MAINWINDOW_H
